<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ～ ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\storehouse\excl;

use catchAdmin\basics\model\Provider;
use catchAdmin\product\model\Product;
use catchAdmin\storehouse\model\WarehouseOut;
use catchAdmin\storehouse\model\WarehouseOutSon;
use catcher\library\excel\ExcelContract;
use PhpOffice\PhpSpreadsheet\Style\Alignment;

class ediWarehouseOutExport implements ExcelContract
{
    protected $model;


    public function __construct(WarehouseOut $WarehouseOutModel)
    {
        $this->model = $WarehouseOutModel;
    }
    /**
     * 设置头部
     *
     * @time 2020年09月08日
     * @return string[]
     */
    public function headers(): array
    {
        // TODO: Implement headers() method.
        return [
            '状态描述',
            '发货仓库',
            '发货仓库编码',
            '发货单日期',
            '发货单号',
            '订单号',
            '客户名称',
            '客户代码',
            '送货客户代码',
            '配送地址',
            '收货人',
            '电话',
            '城市',
            '承运人',
            '承运商编码',
            '运输方式',
            '快递单号',
            '货品代码',
            '货品名称',
            '货品英文名',
            '数量',
            '体积',
            '重量',
            '备注',
            '备注2',
            '发货单总净重',
            '发货单体积'
        ];
    }
//    public function getPushInventoryAttr($value)
//    {
//        $status = [0=>'未推送',1=>'电子口岸已暂存',2=>'电子口岸申报中',3=>'发送海关成功',4=>'发送海关失败',100=>'海关退单',120=>'海关入库', 300=>'人工审核',399=>'海关审结',800=>'放行',899=>'结关',500=>'查验',501=>'扣留移送通关',502=>'扣留移送缉私',503=>'扣留移送法规',599=>'其它扣留',700=>'退运'];
//
//        return array_key_exists($value,$status)?$status[$value]:$value;
//    }

    /**
     * 处理数据
     *
     * @time 2020年09月08日
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @return \think\Collection
     */
    public function sheets()
    {
        $list = $this->model->getEdiExportList();

        $data = [];
        $providerModel = new Provider;
        $outSonModel = new WarehouseOutSon;
        $productModel = new Product;

        foreach ($list as $key => &$value){
            $d = [];
            $edi_json = json_decode($value['edi_json'],true);

            //为空代表是自建订单
            if(empty($edi_json)){continue;}

            $first_line = $edi_json[0];

            $d[0] = $value['status'] == '1' ? '已发货' : '未发货';
            $d[1] = $value['project_client'] == '202407090933417657' ? '佳壹成都仓库' : '佳壹西安仓库';//发货仓库
            $d[2] = $value['project_client'] == '202407090933417657' ? '成都仓库编码' : 'A150002955';//仓库编码
            $d[3] = $value['new_order_date'];  //发货日期
            $d[4] = $value['customer_business_number'];  //发货单号

            $customerCode = $first_line[9];//客户代码
            $customerName = $providerModel->where('account_code',$customerCode)->value('account_name');//客户名称
            $d[5] = '';  //订单号
            $d[6] = $customerName;//客户名称
            $d[7] = $customerCode;//客户代码
            $d[8] = $customerCode;//送货客户代码
            $d[9] = '';//配送地址
            $d[10] = '';//收货人
            $d[11] = '';//电话
            $d[12] = '';  //城市

            $carrierCode =  $first_line[7];//承运方代码
            $carrierName =  $providerModel->where('account_code',$carrierCode)->value('account_name');//承运方名称
            $d[13] = $carrierName;  //承运人
            $d[14] = $carrierCode;  //承运商编码
            $d[15] = '';  //运输方式
            $d[16] = '';  //快递单号
            $d[17] = '';  //货品代码
            $d[18] = '';  //货品名称
            $d[19] = '';  //货品英文名
            $d[20] = '';  //数量

            if('销售出库' ==  $value['customer_order_type']){
                $d[5] = $first_line[52];  //订单号

                $explode_remark = explode('/',$first_line[14]);
                $d[9] = $explode_remark[0];  //配送地址
                $d[10] = $explode_remark[1] ?? '234';  //收货人
                $d[11] = $explode_remark[2] ?? '234';  //电话
                $d[12] = $first_line[21];//城市
                $d[15] = $first_line[13];  //运输方式


            }elseif ('其他出库'){
                $explode_remark = explode('/',$first_line[12]);
                $d[9] = $explode_remark[0];  //配送地址
                $d[10] = $explode_remark[1] ?? '123';  //收货人
                $d[11] = $explode_remark[2] ?? '123';  //电话
                $d[12] = $first_line[19];//城市
                $d[15] = $first_line[11];  //运输方式

            }

            //搜索明细单，搞成多条
            $out_son = $outSonModel->where('warehouse_out_id',$value['warehouse_out_id'])->field('goods_name,out_number')->select();
            foreach ($out_son as $son){
                $d[17] = $son['goods_name'];

                $goods_cname = $productModel->where('goods_name',$d[17])->value('goods_Cname');
                if (preg_match('/^=/', $goods_cname)) {
                    $goods_cname = ' '.$goods_cname;
                }
                $d[18] = $goods_cname;
                $d[20] = (string) $son['out_number'];

                $data[] = $d;
            }
        }
        return $data;
    }

    /**
     * 设置开始行
     *
     * @time 2020年09月08日
     * @return int
     */
    public function setRow()
    {
        return 2;
    }
    /**
     * 设置行宽
     *
     * @time 2020年09月08日
     * @return int
     */
    public function setWidth()
    {
        return [
            "A"=>30,
            "B"=>20,
            "C"=>20,
            "D"=>30,
            "E"=>30,
            "F"=>20,
            "G"=>20,
            "H"=>20,
            "I"=>20,
            "J"=>30,
            "K"=>30,
            "L"=>20,
            "M"=>20,
            "N"=>20,
            "O"=>20,
            "P"=>20,
            "Q"=>20,
            "R"=>20,
            "S"=>20,
            "T"=>30,
            "U"=>30,
            "V"=>30,
            "W"=>30,
            "X"=>30,
            "Y"=>30,
            "Z"=>20,
            "AA"=>20,
            "AB"=>20
        ];
    }

    /**
     * 设置标题
     *
     * @time 2020年09月08日
     * @return array
     */
    public function setTitle()
    {
        return [
            'A1:J1', '物流配送情况跟踪表', Alignment::HORIZONTAL_CENTER
        ];
    }
}